package tiandang.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tiandang.core.config.AuthResource;
import tiandang.core.dao.*;
import tiandang.core.error.BusinessException;
import tiandang.core.model.Promo;
import tiandang.core.model.PromoManager;
import tiandang.core.model.PromoProduct;
import tiandang.core.service.PromoService;
import tiandang.core.validator.ValidatorImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class PromoServiceImpl implements PromoService {

  @Autowired
  private ValidatorImpl validator;

  @Autowired
  private PromoMapper promoMapper;

  @Autowired
  private ProductMapper productMapper;

  @Autowired
  private PromoManagerMapper promoManagerMapper;

  @Autowired
  private PromoProductMapper promoProductMapper;

  @Autowired
  private ManagerMapper managerMapper;

  @Autowired
  private WXFormidMapper wxFormidMapper;

  @Autowired
  private AuthResource resource;


  @Override
  public Object getList(Long startTimestamp, Long endTimestamp, Integer currentPage, Integer limit) {
    Map<String, Object> map = new HashMap<>();
    int offset = (currentPage - 1) * limit;
    List<Promo> promoList = promoMapper.getListByParams(startTimestamp, endTimestamp, limit, offset);
    Long now = new Date().getTime();
    promoList = promoList.stream().map(promo -> {
      if (promo.getStartTimestamp() > now) {
        promo.setStatus(0); //0未开始
      } else if (promo.getEndTimestamp() < now) {
        promo.setStatus(2); //2已结束
      } else {
        promo.setStatus(1); //1进行中
      }
      return promo;
    }).collect(Collectors.toList());

    int totalCount = promoMapper.countByParams(startTimestamp, endTimestamp);
    map.put("promoList", promoList);
    map.put("totalCount", totalCount);

    return map;
  }

  @Override
  public Promo getPromoById(Integer id) {
    return promoMapper.selectByPrimaryKey(id);
  }

  @Override
  @Transactional
  public void save(Promo promo) throws BusinessException {
//    int productTotal = productMapper.countAllProducts();
    List<PromoManager> promoManagerList = promo.getPromoManagerList();
    promo.setCategoryNumber(promo.getPromoProductList().size());
    promo.setManagerNumber(promoManagerList.size());
    promoMapper.insertSelective(promo);

    //把开团团长插入表
    for (PromoManager promoManager : promoManagerList) {
      promoManager.setPromoId(promo.getId());
      promoManagerMapper.insertSelective(promoManager);
    }

    //把开团商品插入表
    List<PromoProduct> promoProductList = promo.getPromoProductList();
    for (PromoProduct promoProduct : promoProductList) {
      promoProduct.setPromoId(promo.getId());
      promoProductMapper.insertSelective(promoProduct);
    }
  }

  @Override
  @Transactional
  public void update(Promo promo) throws BusinessException {
//    int productTotal = productMapper.countAllProducts();
    List<PromoManager> promoManagerList = promo.getPromoManagerList();
    promo.setCategoryNumber(promo.getPromoProductList().size());
    promo.setManagerNumber(promoManagerList.size());
    promoMapper.updateByPrimaryKeySelective(promo);

    //先删除这个团所有的团长
    promoManagerMapper.deleteListByPromoId(promo.getId());

    //再把开团团长插入表
    for (PromoManager promoManager : promoManagerList) {
      promoManager.setPromoId(promo.getId());
      promoManagerMapper.insertSelective(promoManager);
    }

    //先删除这个团所有的商品
    promoProductMapper.deleteListByPromoId(promo.getId());

    //再把开团商品插入表
    List<PromoProduct> promoProductList = promo.getPromoProductList();
    for (PromoProduct promoProduct : promoProductList) {
      promoProduct.setPromoId(promo.getId());
      promoProductMapper.insertSelective(promoProduct);
    }

  }

  @Override
  public void delete(Integer id) {
    promoMapper.deleteByPrimaryKey(id);
  }

  @Override
  public void setIsShow(Integer id, Integer isShow) {
    promoMapper.setIsShow(id, isShow);
  }

  @Override
  public void overNow(Integer id) {
    promoMapper.overNow(id, new Date().getTime());
  }

  @Override
  public void addIncomeAmount(Integer id, BigDecimal income) {
    promoMapper.addIncomeAmount(id, income);
  }

  @Override
  public void minusIncomeAmount(Integer id, BigDecimal income) {
    promoMapper.minusIncomeAmount(id, income);
  }

  @Override
  public List<Promo> getListByIdList(List<Integer> idList) {
    return promoMapper.getListByIdList(idList);
  }

  @Override
  public List<Promo> getListByManagerId(Integer managerId) {
    return promoMapper.getListByManagerId(managerId);
  }

}
